package main

import "fmt"

// Computer 表示电脑对象
type Computer struct {
	CPU     string
	Memory  string
	Storage string
}

// ComputerBuilder 定义电脑构建器接口
type ComputerBuilder interface {
	SetCPU(cpu string) ComputerBuilder
	SetMemory(memory string) ComputerBuilder
	SetStorage(storage string) ComputerBuilder
	Build() *Computer
}

// DesktopBuilder 桌面电脑构建器
type DesktopBuilder struct {
	computer *Computer
}

func NewDesktopBuilder() *DesktopBuilder {
	return &DesktopBuilder{computer: &Computer{}}
}

func (db *DesktopBuilder) SetCPU(cpu string) ComputerBuilder {
	db.computer.CPU = cpu
	return db
}

func (db *DesktopBuilder) SetMemory(memory string) ComputerBuilder {
	db.computer.Memory = memory
	return db
}

func (db *DesktopBuilder) SetStorage(storage string) ComputerBuilder {
	db.computer.Storage = storage
	return db
}

func (db *DesktopBuilder) Build() *Computer {
	return db.computer
}

// Director 指挥者，负责协调构建过程
type Director struct {
	builder ComputerBuilder
}

func NewDirector(builder ComputerBuilder) *Director {
	return &Director{builder: builder}
}

func (d *Director) Construct() *Computer {
	return d.builder.
		SetCPU("Intel Core i7").
		SetMemory("16GB").
		SetStorage("512GB SSD").
		Build()
}

func main() {
	desktopBuilder := NewDesktopBuilder()
	director := NewDirector(desktopBuilder)

	desktop := director.Construct()
	fmt.Printf("CPU: %s\n", desktop.CPU)
	fmt.Printf("Memory: %s\n", desktop.Memory)
	fmt.Printf("Storage: %s\n", desktop.Storage)
}
